﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using System.Configuration;

namespace SistemaPetShop.Negocio
{
    public class Animal
    {
        Conexao c = new Conexao();
        OleDbDataReader dataReaderAccess;
        MySqlDataReader dataReaderMySql;
        StringBuilder query = new StringBuilder();
        EventLog log = new EventLog();
        BaseClass baseClass = new BaseClass();

        public bool _InsereNovoAnimal(string apelido, string raca, string porte, string pelagem, string pacote, string valor,string idCliente,string tEspecie,string sexo)
        {
            try
            {
                c.AbrirConexao(); // AAD

                string dt = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff");

                query.Append("CALL pInsereAnimal ('");
                query.Append(apelido.ToUpper() + "','");
                query.Append(raca.ToUpper() + "','");
                query.Append(porte.ToUpper() + "','");
                query.Append(pelagem.ToUpper() + "','");
                query.Append(pacote.ToUpper() + "','");
                query.Append(baseClass._FormataValorInsereBanco(valor).ToUpper() + "','");
                query.Append(idCliente.ToUpper() + "','");
                query.Append(dt.ToUpper() + "','");
                query.Append(tEspecie.ToUpper() + "','");
                query.Append(sexo.ToUpper());
                query.Append("');");

                bool retorno = c.Insert(query.ToString());

                query.Remove(0, query.Length);

                 c.FecharConexao(); // AAD

                return retorno;

            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                throw;
            }

        }

        public DataTable _ListaAnimal(Int32 idCliente)
        {
            DataTable dt = new DataTable();
            c.AbrirConexao();
            dataReaderMySql = c.Consulta("select animal.IdAnimal,animal.apelido,animal.sexo,animal.Especie,animal.Raca,animal.porte,animal.pelagem,animal.Valor,animal.dataCadastro from Animal animal where animal.Cliente_idCliente = " + idCliente.ToString());
            dt.Load(dataReaderMySql);
            c.FecharConexao();
            return dt;
        }

        public DataTable _ListaNomeAnimal(Int32 idCliente)
        {
            DataTable dt = new DataTable();
            c.AbrirConexao();
            dataReaderMySql = c.Consulta("select IdAnimal,apelido from Animal where Cliente_idCliente = " + idCliente.ToString());
            dt.Load(dataReaderMySql);
            c.FecharConexao();
            return dt;
        }


        public bool _AtualizaCaminhoFoto(Int32 idAnimal,String caminho)
        {
            c.AbrirConexao();

            string ca = caminho.Replace("\\","/");

            bool ret = c.Insert("update Animal set foto = '" + ca.ToString().ToUpper() + "' where idAnimal = " + idAnimal.ToString());
            c.FecharConexao();
            return ret;
        }

        public int _ConsultaIdAnimal()
        {
            int idAnimal;
            c.AbrirConexao();
            dataReaderMySql = c.Consulta("select max(idAnimal) from Animal;");
            dataReaderMySql.Read();
            idAnimal = dataReaderMySql.GetInt32(0);
            c.FecharConexao();
            return idAnimal;
        }

        public string _ConsultaFoto(Int32 idAnimal)
        {
            string caminho = "0";
            c.AbrirConexao();
            dataReaderMySql = c.Consulta("select foto from Animal where idAnimal = " + idAnimal.ToString());
            if (dataReaderMySql.Read())
            {
                if (dataReaderMySql[0] != DBNull.Value)
                {
                    caminho = dataReaderMySql.GetString(0);
                }
            }
            
            dataReaderMySql.Close();
            c.FecharConexao();
            return caminho;

        }

        public bool _ApagaAnimal(Int32 idAnimal)
        {
            bool ret = false;
            try
            {
                c.AbrirConexao();
                ret = c.Insert("DELETE FROM ANIMAL WHERE idAnimal = " + idAnimal.ToString());
                c.FecharConexao();
                return ret;
            }
            catch (Exception ex)
            {
                log.Source = ConfigurationSettings.AppSettings.Get("eventView").ToString();
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
                return ret;
                throw;
            }

        }


    }
}
